<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useCounterStore } from "@/stores/counter.js"
import service from '@/utils/request'
import { ElMessage } from 'element-plus'

// 登录表单绑定数据
let form = ref({ username: 'admin', password: 'admin123' })
let counter = useCounterStore()
const router = useRouter()

// 登录方法
function login() {
  service.post("/user/login?username=" + form.value.username + "&password=" + form.value.password).then(
      res => {
        if (res.data != null) {
          counter.vo_user.value = res.data
          router.replace({ path: "/" })
        } else {
          ElMessage.error("用户名或密码错误，请重新输入")
        }
      }
  )
}
</script>

<template>
  <div class="login-page">
    <div class="login-box">
      <h2>医院管理系统登录</h2>
      <el-form :model="form" label-width="80px" status-icon>
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="login-button">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
/* 整体页面背景 */
.login-page {
  height: 100vh;
  background: linear-gradient(to right, #cbe7ff, #ecf5ff);
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录框美化 */
.login-box {
  background: white;
  padding: 40px 50px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  width: 400px;
}

/* 标题样式 */
.login-box h2 {
  text-align: center;
  color: #409EFF;
  margin-bottom: 30px;
  font-weight: 600;
  font-size: 24px;
  font-family: 'Microsoft YaHei', sans-serif;
}

/* 登录按钮样式 */
.login-button {
  width: 100%;
  font-size: 16px;
  letter-spacing: 2px;
}
</style>
